﻿@page "/Client"
@inject Blazored.LocalStorage.ISyncLocalStorageService localStorage
@inject NavigationManager NavigationManager
@inject HttpClient Http

@using System.Text.Json
@using BlazorApp1.Model

<h1>欢迎来到疫情防控上报系统</h1>

<SurveyPrompt Title="" />

@code
{ protected override async Task OnInitializedAsync()
    {
        string token = localStorage.GetItem<string>("token");
        string rtoken = localStorage.GetItem<string>("rtoken");
        if (token == null)
        {
            NavigationManager.NavigateTo("/client/login");
        }
        else
        {
           Http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer" ,localStorage.GetItem<string>("token"));
           var res = await Http.PostAsync("/api/_Account/RefreshToken?refreshToken=" + rtoken,null);
            if (res.IsSuccessStatusCode == true)
            {
                var jwt = JsonSerializer.Deserialize<Token>(await res.Content.ReadAsStringAsync());
                localStorage.SetItem("token", jwt.access_token);
                localStorage.SetItem("rtoken", jwt.refresh_token);
            }
            else
            {
             NavigationManager.NavigateTo("/client/login");
            }

        }
    }
}
